package com.kyzh.core.adapters.providers

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.widget.Button
import com.chad.library.adapter.base.entity.node.BaseNode
import com.chad.library.adapter.base.provider.BaseNodeProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gushenge.core.beans.GiftList
import com.gushenge.core.impls.GameImpl
import com.gushenge.core.listeners.ResultListener
import com.kyzh.core.R
import com.kyzh.core.uis.alert
import com.kyzh.core.utils.startLogin
import com.gushenge.core.toast


class GiftItemProvider() :BaseNodeProvider(){
    override val itemViewType: Int
        get() = 1

    override val layoutId: Int
        get() = R.layout.gift_item_2
    override fun convert(helper: BaseViewHolder, item: BaseNode) {
        val x = item as GiftList.X
        helper.setText(R.id.tvType, x.cardname)
                .setText(R.id.tvDesc, "${context.getString(R.string.giftDesc)}: ${x.content}")
                helper.getView<Button>(R.id.btGet).setOnClickListener {
                    if (context.startLogin()) {
                        GameImpl.getGift(x.id, object : ResultListener {
                            override fun success(bean: Any) {
                                bean as String
                                context.alert(context.getString(R.string.giftCode), bean, context.getString(R.string.copy), noButtonText = null,{
                                    val manager = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
                                    manager.setPrimaryClip(ClipData.newPlainText("data", bean))
                                    context.getString(R.string.copyGiftCode).toast()
                                },{})
                            }

                            override fun error(error: String) {
                                error.toast()
                            }
                        })
                    }
                }

    }
}